<template>
<div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 警告区域 -->
      <el-alert
        title="注意：只允许为第三级分类设置相关参数!"
        type="warning" show-icon :closable="false"
      ></el-alert>
      <!-- 选择商品分类区域 -->
      <el-row class="cat_opt">
          <el-col>
              <span>选择商品分类：</span>
              <el-cascader
                props.expand-trigger="hover"
                :options="cateList"
                :props="cateProps"
                v-model="selectedCateKeys"
                @change="handelChange"
                clearable
            ></el-cascader>
          </el-col>
      </el-row>
      <el-tabs v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane label="动态参数" name="many">
            <el-button
            type="primary"
            size="mini"
            :disabled="isBtnDisabled"
            @click="addDialogVisible = true"
            >添加参数</el-button>
            <el-table :data="manyTableData" border stripe>
                <el-table-column type="expand"></el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                    type="primary"
                    size="small"
                    @click="showEditDialog(scope.row.attr_id)"
                    >编辑</el-button
                    >
                    <el-button
                    type="danger"
                    size="small"
                    @click="RemoveParams(scope.row.attr_id)"
                    >删除</el-button
                    >
                </template>
                </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
            <el-button
            type="primary"
            size="mini"
            :disabled="isBtnDisabled"
            @click="addDialogVisible = true"
            >添加属性</el-button>
            <el-table :data="onlyTableData" border stripe>
                <el-table-column type="expand"></el-table-column>
                <el-table-column type="index"></el-table-column>
                <el-table-column label="参数名称" prop="attr_name"></el-table-column>
                <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                    type="primary"
                    size="small"
                    @click="showEditDialog(scope.row.attr_id)"
                    >编辑</el-button
                    >
                    <el-button
                    type="danger"
                    size="small"
                    @click="RemoveParams(scope.row.attr_id)"
                    >删除</el-button
                    >
                </template>
                </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <el-dialog
        :title="titleText"
        :visible.sync="addDialogVisible"
        width="50%"
        :before-close="handleClose">
        <span>这是一段信息</span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
        </el-dialog>
    </el-card>
</div>

</template>

<script>
    export default {
        data() {
            return {
                cateList:[],
                // 级联选择框的配置对象
                cateProps: {
                    value: 'cat_id',
                    label: 'cat_name',
                    children: 'children'
                },
                // 级联选择框双向绑定到的数组
                selectedCateKeys:[],
                activeName:'many',
                // 动态参数数据
                manyTableData: [],
                // 静态属性数据
                onlyTableData: [],
                addDialogVisible:false
            }
        },
        created(){
            this.getCateList()
        },
        methods:{
            async getCateList(){
                const {data: res} = await this.$http.get('categories')
                if (res.meta.status !== 200){
                    return this.$message.error('获取商品分类失败！')
                }
                this.cateList = res.data
                console.log(this.cateList)
            },
            handelChange(){
                this.getParamsData()
            },
            handleTabClick(){
                this.getParamsData()  
            },
            // 获取参数的列表数据
            async getParamsData(){
                if(this.selectedCateKeys.length!==3){
                    this.selectedCateKeys=[]
                    return
                }
                // 根据三级分类的id和当前所在的Tabs，获取对应的参数
                const { data: res } = await this.$http.get(
                    `categories/ ${this.cateId}/attributes`,
                    {
                    params: { sel: this.activeName }
                    }
                )
                if (res.meta.status !== 200) {
                    this.$message.error('获取参数列表失败!')
                }
                console.log(res.data)
                // 判断获取到的数据是否是当前tabs需要的数据
                if (this.activeName === 'many') {
                    this.manyTableData = res.data
                } else {
                    this.onlyTableData = res.data
                }
            },
            
        },
        computed:{
            isBtnDisabled(){
                if(this.selectedCateKeys.length!==3){
                    return true
                }
                return false
            },
            cateId() {
                if (this.selectedCateKeys.length === 3) {
                    return this.selectedCateKeys[2]
                }
                return null
            },
            titleText() {
                if (this.activeName === 'many') {
                    return '添加动态参数'
                } else {
                    return '添加静态属性'
                }
            }
        }
    }
</script>

<style lang="less" scoped>
.cat_opt {
  margin: 20px 0;
}
.el-tag {
  margin-left: 10px;
}
.input-new-tag {
  margin-left: 20px;
  width: 120px;
}
.button-new-tag {
  margin-left: 20px;
}
</style>